﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.订单一览新.销售确认单;
using DXApplicationTangche.service;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.标签打印.Service;
using DXApplicationTangche.UC.下单MTO.Service;
using mendian;
using DXApplicationTangche.Utils;

namespace DXApplicationTangche.UC.标签打印
{
    public partial class Form发货贴带详情 : DevExpress.XtraEditors.XtraForm
    {
        private List<CustomerTagRecordDTO> barcodeRecordDTOs = new List<CustomerTagRecordDTO>();
        public Form发货贴带详情()
        {
            InitializeComponent();
            this.textBox扫码.Enabled = false;
        }
        public Form发货贴带详情(String batchNo,String salesType)
        {
            InitializeComponent();
            this.textBox扫码.Enabled = false;
            //区分是否为上海订单
            //上海Luna订单发运
            if ("LUNA下单".Equals(salesType))
            {
                this.textBox上海发货扫批号.Text = batchNo;
                this.textBox生产批号.Enabled = false;
                this.textBox扫码.Enabled = false;
                this.ActiveControl = this.textBox上海发货扫批号;
            }
            //除上海订单，其他订单发运
            else
            {
                this.textBox生产批号.Text = batchNo;
                this.textBox上海发货扫批号.Enabled = false;
                this.ActiveControl = this.textBox生产批号;
            }
        }
        private void Form发货贴带详情_Load(object sender, EventArgs e)
        {
            this.ActiveControl = this.textBox生产批号;
        }

        private void textBox上海发货扫批号_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrWhiteSpace(this.textBox上海发货扫批号.Text))
                {
                    MessageBox.Show("生产批号为空");
                    return;
                }
                try
                {
                    SfExpressMessageResult sfExpressMessageResult = new SfExpressMessageResult();
                    if (this.checkBox是否两联.Checked)
                    {
                        sfExpressMessageResult = 发运Service.sfExpressOrderByBatchNo(2, this.textBox上海发货扫批号.Text.Trim());
                    }
                    else
                    {
                        sfExpressMessageResult = 发运Service.sfExpressOrderByBatchNo(3, this.textBox上海发货扫批号.Text.Trim());
                    }
                    if (sfExpressMessageResult.errorCode.Contains("200"))
                    {
                        FileUtil.DownloadPicture("http://192.168.3.33:4545/sfexpress/" + this.textBox上海发货扫批号.Text.Trim() + ".jpg", @"pic\" + this.textBox上海发货扫批号.Text.Trim() + ".jpg", -1);
                        System.Diagnostics.Process.Start(@"pic\" + this.textBox上海发货扫批号.Text.Trim() + ".jpg");
                    }
                    else
                    {
                        MessageBox.Show(sfExpressMessageResult.errorMessage);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                this.textBox上海发货扫批号.Clear();
                this.ActiveControl = this.textBox生产批号;
            }
        }

        private void textBox生产批号_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrWhiteSpace(this.textBox生产批号.Text))
                {
                    MessageBox.Show("生产批号为空");
                    return;
                }
                try
                {
                    SfExpressMessageResult sfExpressMessageResult = new SfExpressMessageResult();
                    if (this.checkBox是否两联.Checked)
                    {
                        sfExpressMessageResult = 发运Service.sfExpressOrderByBatchNoForKashiyama(this.textBox生产批号.Text.Trim(), 2);
                    }
                    else
                    {
                        sfExpressMessageResult = 发运Service.sfExpressOrderByBatchNoForKashiyama(this.textBox生产批号.Text.Trim(), 3);
                    }
                    FunctionHelper.JsonSerialization(sfExpressMessageResult);
                    //  200为坚山直接发运打印顺丰贴
                    if (ConstPlainMessageResult.OK.Equals(sfExpressMessageResult.errorCode))
                    {
                        FileUtil.DownloadPicture("http://192.168.3.33:4545/sfexpress/" + this.textBox生产批号.Text.Trim() + ".jpg", @"pic\" + this.textBox生产批号.Text.Trim() + ".jpg", -1);
                        System.Diagnostics.Process.Start(@"pic\" + this.textBox生产批号.Text.Trim() + ".jpg");
                        this.重置();
                    }
                    //  500为其他订单，需要扫成衣码发运
                    else if (ConstPlainMessageResult.INTENAL_SERVER_ERROR.Equals(sfExpressMessageResult.errorCode))
                    {
                        this.textBox生产批号.Enabled = false;
                        this.textBox扫码.Enabled = true;
                        this.ActiveControl = this.textBox扫码;
                    }
                    //  404未找到订单
                    else
                    {
                        MessageBox.Show("没找到订单，请联系李金健");
                        this.重置();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        
        private void textBox扫码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrWhiteSpace(this.textBox扫码.Text))
                {
                    MessageBox.Show("成衣码为空");
                    return;
                }
                CustomerTagRecordDTO barcodeRecords =
                    销售确认Service.getCustomerTagRecordByBarcode(this.textBox扫码.Text.Trim()).barcodeRecords;
                if (!String.IsNullOrEmpty(barcodeRecords.styleSizeCd))
                {
                    try { barcodeRecords.styleSizeCd = barcodeRecords.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
                if (!String.IsNullOrEmpty(barcodeRecords.barcode))
                {
                    if (this.barcodeRecordDTOs.Count > 0)
                    {
                        if (!this.barcodeRecordDTOs.Exists(dto => dto.barcode.Equals(barcodeRecords.barcode)))
                        {
                            this.barcodeRecordDTOs.Add(barcodeRecords);
                        }
                    }
                    else
                    {
                        this.barcodeRecordDTOs.Add(barcodeRecords);
                    }
                }
                else
                {
                    MessageBox.Show("扫描成衣码不存在");
                }
                this.gridControl代发货一览.DataSource = this.barcodeRecordDTOs;
                this.gridView代发货一览.RefreshData();
                this.gridView代发货一览.BestFitColumns();
                this.textBox扫码.Clear();
            }
        }

        private void simpleButton发运_Click_1(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(this.textBox生产批号.Text))
            {
                MessageBox.Show("请先扫描生产批号");
                this.ActiveControl = this.textBox生产批号;
                return;
            }
            if (barcodeRecordDTOs.Count <= 0)
            {
                this.ActiveControl = this.textBox扫码;
                MessageBox.Show("请先扫描成衣码确认发运成衣详情");
                return;
            }
            List<String> barcodes = new List<String>();
            foreach (CustomerTagRecordDTO item in this.barcodeRecordDTOs)
            {
                barcodes.Add(item.barcode);
            }
            try
            {
                SfExpressMessageResult sfExpressMessageResult = new SfExpressMessageResult();
                if (this.checkBox是否两联.Checked)
                {
                    sfExpressMessageResult = 发运Service.sfExpressOrderByBatchNoAndBarcodes(this.textBox生产批号.Text.Trim(), 2, barcodes);
                }
                else
                {
                    sfExpressMessageResult = 发运Service.sfExpressOrderByBatchNoAndBarcodes(this.textBox生产批号.Text.Trim(), 3, barcodes);
                }
                FunctionHelper.JsonSerialization(sfExpressMessageResult);
                if (("200").Equals(sfExpressMessageResult.errorCode))
                {
                    FileUtil.DownloadPicture("http://192.168.3.33:4545/sfexpress/" + this.textBox生产批号.Text.Trim() + ".jpg", @"pic\" + this.textBox生产批号.Text.Trim() + ".jpg", -1);
                    System.Diagnostics.Process.Start(@"pic\" + this.textBox生产批号.Text.Trim() + ".jpg");
                    this.textBox扫码.Enabled = false;
                }
                else
                {
                    MessageBox.Show(sfExpressMessageResult.errorMessage);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void 重置()
        {
            this.textBox扫码.Enabled = false;
            this.textBox生产批号.Enabled = true;
            this.textBox生产批号.Clear();
            this.ActiveControl = this.textBox生产批号;
            this.barcodeRecordDTOs.Clear();
            this.gridView代发货一览.RefreshData();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String barcode = this.gridView代发货一览.GetRowCellValue(this.gridView代发货一览.FocusedRowHandle, "barcode").ToString();
                for (int i = this.barcodeRecordDTOs.Count - 1; i >= 0; i--)
                {
                    if (barcode.Equals(this.barcodeRecordDTOs[i].barcode))
                    {
                        this.barcodeRecordDTOs.Remove(this.barcodeRecordDTOs[i]);
                    }
                }
                //this.gridControl代发货一览.DataSource = this.barcodeRecordDTOs;
                this.gridView代发货一览.RefreshData();
                this.gridView代发货一览.BestFitColumns();
            }
            catch
            {
                MessageBox.Show("修改订单状态失败");
            }
        }

        private void simpleButton重置_Click(object sender, EventArgs e)
        {
            this.重置();
        }
    }
}